package fy.form.groovy;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.bo.V3xOrgPost;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.v3x.services.V3XLocator;

public class GroovyFunc2 {

    private static final Log logger = LogFactory.getLog(GroovyFunc2.class);
    // private static final int CHECK_IS_AUDIT = 0;// 查看人员是否审批人员
    // private static final int CHECK_SHENHECISHU = 1;// 非审批人员需要审批几次
    private static final long shenpi1 = 2598272894397269345L;
    private static final long shenpi2 = 2598242906183847131L;
    private static final long shenpi3 = 1325558360490659349L;
    private static final long shenpi4 = 5502099106103904405L;

    private static int check(long deptID, long postid, long userID, int result,
            OrgManager orgManager) throws Exception {
        List<V3xOrgMember> members = orgManager.getMembersByDepartmentPost(
                deptID, postid);
        if (members.size() > 0) {
            for (int i = 0, size = members.size(); i < size; i++) {
                if (userID == members.get(i).getId()) {
                    return result - 1;
                }
            }
            return result;
        }
        return -2;
    }

    private static boolean post(long deptID, long postID, long userID,
            OrgManager orgManager) throws Exception {
        List<V3xOrgMember> members = orgManager.getMembersByDepartmentPost(
                deptID, postID);
        if (members.size() > 0) {
            for (int i = 0, size = members.size(); i < size; i++) {
                if (userID == members.get(i).getId()) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取需审批次数
     *
     * @param code
     * @param loginAccountName
     * @return
     */
    public static int getAuditCount(Object object) {
        // System.out.println(object);
        // logger.info(object);
        // String loginName, String loginAccountName
        // return fy.form.groovy.GroovyFunc.getAuditCount(param);
        Object[] array = (Object[]) object;

        if ("Account|5686590792835548528".equals(array[1])) {
            try {
                String loginUserID = (String) array[0];
                String loginAccountID = (String) array[1];
                loginUserID = loginUserID.replace("Member|", "");
                loginAccountID = loginAccountID.replace("Account|", "");
                OrgManager orgManager = V3XLocator.getInstance().lookup(
                        OrgManager.class);
                long userID = Long.valueOf(loginUserID);
                long accountID = Long.valueOf(loginAccountID);
                List depList = new ArrayList();
                V3xOrgMember member = orgManager
                        .getMemberById(userID);
                long deptId = member.getOrgDepartmentId();
                depList = orgManager.getUserDomainIDs(userID,
                        accountID,
                        new String[]{"Department"});
                if (depList.size() > 0) {
                    V3xOrgDepartment dept = (V3xOrgDepartment) depList.get(0);
                    if (dept != null) {
                        deptId = (dept.getId());
                    }
                }
                List<V3xOrgPost> posts = orgManager.getDepartmentPost(deptId);
                boolean exist = false;
                for (int i = 0, size = posts.size(); i < size; i++) {
                    long postID = posts.get(i).getId();
                    if (shenpi4 == postID || shenpi3 == postID
                            || shenpi2 == postID || shenpi1 == postID) {
                        exist = true;
                    }
                }
                if (!exist) {
                    V3xOrgDepartment pd = orgManager.getParentDepartment(deptId);
                    if (pd != null) {
                        deptId = pd.getId();
                    }
                }
//                long userID = member.getId();
                int result = 0;
                if ((result = check(deptId, shenpi4, userID, 4, orgManager)) != -2) {
                    if (post(deptId, shenpi1, userID, orgManager)) {
                        result = 0;
                    } else if (post(deptId, shenpi2, userID, orgManager)) {
                        result = 1;
                    } else if (post(deptId, shenpi3, userID, orgManager)) {
                        result = 2;
                    }
                } else if ((result = check(deptId, shenpi3, userID, 3,
                        orgManager)) != -2) {
                    if (post(deptId, shenpi1, userID, orgManager)) {
                        result = 0;
                    } else if (post(deptId, shenpi2, userID, orgManager)) {
                        result = 1;
                    }
                } else if ((result = check(deptId, shenpi2, userID, 2,
                        orgManager)) != -2) {
                    if (post(deptId, shenpi1, userID, orgManager)) {
                        result = 0;
                    }
                } else if ((result = check(deptId, shenpi1, userID, 1,
                        orgManager)) != -2) {
                }
                return (result);
            } catch (Exception e) {
                logger.error("需审批次数获取异常", e);
            }
        }
        return -1;
    }

    // public static int getAuditCount1(Object object) {
    // return getAuditCount(object);
    // }
    // public static String getObjectStyle(Object object) {
    // Object[] array = (Object[]) object;
    //
    // System.out.println(array[0]);
    // logger.info(array[0]);
    // System.out.println(array[1]);
    // logger.info(array[1]);
    // return "12";
    // }
    public static int getFengGuan(Object object) {

        Object[] array = (Object[]) object;
        if ("Account|5686590792835548528".equals(array[1])) {
            try {
                String loginUserID = (String) array[0];
                String loginAccountID = (String) array[1];
                loginUserID = loginUserID.replace("Member|", "");
                loginAccountID = loginAccountID.replace("Account|", "");
                long userID = Long.valueOf(loginUserID);
                long accountID = Long.valueOf(loginAccountID);
                OrgManager orgManager = V3XLocator.getInstance().lookup(
                        OrgManager.class);
                List depList = new ArrayList();
                V3xOrgMember member = orgManager
                        .getMemberById(userID);
                long deptId = member.getOrgDepartmentId();
                depList = orgManager.getUserDomainIDs(userID, accountID,
                        new String[]{"Department"});
                if (depList.size() > 0) {
                    V3xOrgDepartment dept = (V3xOrgDepartment) depList.get(0);
                    if (dept != null) {
                        deptId = (dept.getId());
                    }
                }
                long postID = -803245032428191421L;// 分管岗位ID
                if (orgManager.getMembersByDepartmentPost(deptId, postID)
                        .size() > 0) {
                    return 1;
                }
                String parentPath = orgManager.getDepartmentById(deptId)
                        .getParentPath();
                V3xOrgDepartment parentDept = null;

                for (int i = 0; i < 5; i++) {
                    if (parentPath == null || "".equals(parentPath)
                            || "0".equals(parentPath)) {
                        break;
                    }

                    parentDept = orgManager.getDepartmentByPath(parentPath);
                    if (parentDept == null) {
                        break;
                    }

                    if (orgManager.getMembersByDepartmentPost(
                            parentDept.getId(), postID).size() > 0) {
                        return 1;
                    }
                    if ("0000000103000020".equals(parentPath)) {
                        break;
                    }
                    parentPath = parentDept.getParentPath();

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    // ///////////////////
    private static long[] deptArray = {2869001411135357909L,// 基建
        -3602358740144142925L,// 信息部
        7815628411628440028L,// 行政
        2772581170730454430L,// 配件
        6258262779454245335L,// 人事
        87988775416582016L, // 质管
        1097534762777136334L,//宣传部
        -7370705342937462L//供应管理部
    //			487067892445627598L // 研究院
    };

    private static boolean checkZongcai(long deptID) {
        for (int i = 0, size = deptArray.length; i < size; i++) {
            if (deptArray[i] == deptID) {
                return true;
            }
        }
        return false;
    }

    public static int zongcaiaudit(Object object) {
        // return fy.form.groovy.GroovyFunc.getAuditCount(param);
        // return fy.form.groovy.GroovyFunc.getFengGuan(param);
        // return fy.form.groovy.GroovyFunc.zongcaiaudit(param);

        Object[] array = (Object[]) object;
        if ("Account|5686590792835548528".equals(array[1])) {
            try {

                String deptName = (String) array[0];
//                String loginAccountID = (String) array[1];
                deptName = deptName.replace("Department|", "");
//                loginAccountID = loginAccountID.replace("Account|", "");
                long deptID = Long.valueOf(deptName);
//                long accountId = Long.valueOf(loginAccountID);
//                String loginAccountName = (String) array[1];
                OrgManager orgManager = V3XLocator.getInstance().lookup(
                        OrgManager.class);
//                List<V3xOrgDepartment> list = orgManager.getDepartmentsByName(
//                        deptName, accountId);
                V3xOrgDepartment dept = null;
                for (int i = 0; i < 4; i++) {
                    if (checkZongcai(deptID)) {
                        return 1;
                    } else {
                        dept = orgManager.getParentDepartment(deptID);
                        if (dept != null) {
                            deptID = dept.getId();
                            dept = null;
                        } else {
                            dept = null;
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }
}
//{"_currentDiv":{"_currentDiv":"0"},"mainbodyDataDiv_0":{"id":"-1","createId":"0","createDate":"2015-10-03 14:47:58.868","modifyId":"-1825607432309486470","modifyDate":"2015-11-02 14:30:25.097","moduleType":"36","moduleId":"0","contentType":"20","moduleTemplateId":"-4165775831353899684","contentTemplateId":"-7392070433096708211","sort":"1","title":"事项跟踪","content":"","rightId":"8476042826308209520","status":"STATUS_RESPONSE_VIEW","viewState":"1","contentDataId":"4636092790629113753"},"formmain_3965":{"field0002":"11111111","field0003":"222222222","field0004":"2333333","field0011":"","field0008":"","field0009":"","field0010":"","field0006":"","field0007":"","field0007_0_editAtt":"true","field0012":"","field0005":""},"attachmentInputs":[]}
